package com.wang.client;

import com.wang.util.RequestUtil;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * 模拟客户端发送请求
 * Created by wang on 2017/5/3.
 */
public class Client {
    public static void sendRequest(){
        Socket socket = null;
        OutputStream outputStream = null;
        PrintWriter out = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            //创建客户端 sokect
            socket = new Socket("localhost", 8886);
            //获取输出流，向服务器发送请求
            outputStream = socket.getOutputStream();
            out = new PrintWriter(outputStream);
            out.write("我的请求：" + System.currentTimeMillis());
            out.flush();
            socket.shutdownOutput();
            //获取输入流，读取响应信息
            inputStream = socket.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String info;
            while ((info = bufferedReader.readLine()) != null) {
                System.out.println("服务器："+info);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try {
                if (outputStream != null)
                    outputStream.close();
                if (out!=null)
                    out.close();
                if (socket!=null)
                    socket.close();
                if (inputStream!=null)
                    inputStream.close();
                if (bufferedReader!=null)
                    bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
